package com.suitsoft.ut.service.commons;

import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.suitsoft.ut.entity.thesis.Thesis;

/**
 * 抽查 服务层类
 *@author Suineg
 */
@Service
@Transactional
public class SpotCheckManager {
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	/**
	 * 获取随机被选的论文
	 * @param thesises
	 * @param cnt
	 * @param percent
	 * @return
	 */
	public List<Thesis> random(List<Thesis> thesises,Integer cnt,float percent){
		List<Thesis> list = Lists.newArrayList();
		Set<Integer> indxSet = null;
		if(cnt != null){
			indxSet = randomByCnts(thesises.size(), cnt);
		}else{
			int actual = (int)(thesises.size() *percent); 
			indxSet = randomByCnts(thesises.size(), actual);
		}
		for(Integer i : indxSet){
			list.add(thesises.get(i));
		}
		return list;
	}
	
	/**
	 * 按人数获取 随机索引
	 * @param cnt
	 * @return
	 */
	private  Set<Integer> randomByCnt(Integer listSize,Integer cnt){
		Set<Integer> result = new HashSet<Integer>();
		Random r = new Random();
		for(int i = 0; i < cnt;i ++){
			result.add(r.nextInt(listSize)) ;
		}
		System.out.println(result);
		return result;
	}
	
	/**
	 * 按人数获取 随机索引
	 * @param cnt
	 * @return
	 */
	private  Set<Integer> randomByCnts(Integer listSize,Integer cnt){
		Set<Integer> set = new HashSet<Integer>();
		set.addAll(randomByCnt(listSize,cnt));
		Set<Integer> s = new HashSet<Integer>();
		if(cnt != set.size()){
			set.addAll(randomByCnt(listSize,cnt));
			int f = 1;
			for(Integer i : set){
				s.add(i);
				if(f ++ >= cnt){
					break;
				}
			}
		}else{
			s.addAll(set);
		}
		System.out.println(s);
		return s;
	}
}

